// components/picker-tool/s-picker.js
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        options1: {
            type: Array,
            value: []
        },
        options11: {
            type: Array,
            value: [],
        },
        picker_value: {
            type: String,
            value: "",
        },
        label_val: {
            type: String,
            value: "",
        },
        search_label_val: {
            type: String,
            value: "",
        },
        picker_disabled: {
            type: Boolean,
            value: false,
        },
        picker_required: {
            type: Boolean,
            value: true,
        },
        value_key: {
            type: String,
            value: "",
        },
        picker_placeholder: {
            type: String,
            value: '请点击选择'
        },
        custom_style: {
            type: String,
            value: ''
        },
        title_width: {
            type: String,
            value: '5rem'
        },
        picker_size: {
            type: String,
            value: ''
        },
        handle_show: {
            type: Boolean,
            value: false,
        },
        scan_show: {
            type: Boolean,
            value: false,
        },
        icon: {
            type: String,
            value: "arrow-down"
        },
        input_align: {
            type: String,
            value: "left"
        },
        in_placeholder: {
            type: String,
            value: "手动输入"
        }


    },

    /**
     * 组件的初始数据
     */
    data: {
        search_value: "",
        popup_show1: false,

    },

    /**
     * 组件的方法列表
     */
    methods: {
        clearDatas() {
            let arr = this.data.options11
            this.setData({
                search_value: "",
                options1: arr
            })
        },
        onClickItem(e) {
            console.log(e);
            if (e._relatedInfo.anchorTargetText.length > 10) {
                wx.showModal({
                    title: '详情',
                    content: e._relatedInfo.anchorTargetText,
                    confirmText: "关闭",
                    showCancel: false,
                    complete: (res) => {
                        if (res.cancel) {
                        }
                        if (res.confirm) {
                        }
                    }
                })
            }
        },
        onSearch1(e) {
            if (e.detail == "") {
                let arr = this.data.options11
                this.setData({
                    options1: arr
                })
            } else {
                if (this.data.value_key) {
                    let list1 = this.data.options11
                    let arr = []
                    for (let i = 0; i < list1.length; i++) {
                        if (list1[i][this.data.value_key].match(this.data.search_value) != null) {
                            arr.push(list1[i]);
                        }
                    }
                    this.setData({
                        options1: arr
                    })
                } else {
                    let list1 = this.data.options11
                    let arr = []
                    for (let i = 0; i < list1.length; i++) {
                        if (list1[i].match(this.data.search_value) != null) {
                            arr.push(list1[i]);
                        }
                    }
                    this.setData({
                        options1: arr
                    })
                }
            }
        },

        // 点击----选取输入框
        onClick1(event) {
            if (this.data.picker_disabled != true) {
                this.setData({
                    popup_show1: true
                })
            }
            this.triggerEvent("setPickerShow", false);
            // event.detail 为当前输入的值
            // console.log(event.detail);

        },
        // 选择----输入框关闭
        onClose1() {
            this.setData({ popup_show1: false });
            this.triggerEvent("setPickerValueClose", this.data.picker_value);
            this.triggerEvent("setPickerShow", true);
        },
        // 确认----弹出框关闭
        onChange1(event) {
            const { picker, value, index } = event.detail;
            console.log("value:", value);
            this.setData({
                picker_value: value,
                popup_show1: false,
            })
            this.triggerEvent("setPickerValue", value);
            this.triggerEvent("setPickerShow", true);

        },
        // 点击选项
        employeePopupChange(e) {
            console.log(e);
            let value = e.currentTarget.dataset.row
            this.setData({
                picker_value: value,
                popup_show1: false,
            })
            this.triggerEvent("setPickerValue", value);
            this.triggerEvent("setPickerShow", true);
        },



        // 手动输入值
        confirmValue() {
            let value = this.data.handle_picker_value
            this.triggerEvent("scanPickerValue", value);
            this.setData({
                popup_show1: false,
            })
            this.triggerEvent("setPickerShow", true);

        },
        // 点击“详情”按钮：仅查看内容，不选择
        onViewDetail(event) {
            const row = event.currentTarget.dataset.row;
            wx.showModal({
                title: '详细信息',
                content: row,
                showCancel: true,
                confirmText: '关闭',
                showCancel:false,
                success: (res) => {
                    if (res.confirm) {
                    }
                }
            });

        },
        // 扫描输入值
        scan1() {
            const self = this;
            wx.scanCode().then((res) => {
                const label_code = res.result;
                wx.showLoading({
                    title: "loading"
                })
                console.log(label_code);
                setTimeout(function () {
                    wx.hideLoading()
                    self.setData({
                        picker_value: label_code,
                    })
                    self.triggerEvent("scanPickerValue", label_code);


                }, 500)
            }).catch(err => {
                console.log(err);
            });
        },

    }
})
